package com.example.pacs.dicom.config;

import com.example.pacs.dicom.server.cfind.MWLCFindSUP;
import com.example.pacs.dicom.server.cmove.CMOVESUP;
import com.example.pacs.dicom.server.cstoresup.DicomServer;
import com.example.pacs.dicom.server.cstoresup.SUPCStoreServiceImpl;
import com.google.common.collect.Maps;
import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;

@Configuration
public class BeanConfiguration {

    /**
     * dcm文件存放的位置
     */
    @Value("${pacs.storage.dir}")
    private String pacsStorageDir;

    /**
     * pacs系统的application entity title
     */
    @Value("${pacs.ae.title}")
    private String pacsAeTitle;

    /**
     * pacs系统监听的端口号,多个用逗号分隔
     */
    @Value("#{'${pacs.ae.ports}'.split(',')}")
    private List<Integer> pacsPorts;

    @Bean
    public EventBus getEventBus() {
        return new AsyncEventBus(Executors.newFixedThreadPool(100));
    }

    @Resource
    private SUPCStoreServiceImpl storeService;

    @Resource
    private MWLCFindSUP mwlcFindSUP;

    @Resource
    private CMOVESUP cmovesup;

    /**
     * 通过初始化方法得到DICOM server map,并注入到IOC容器
     * 这里map主要是因为会有多个端口
     */
    @Bean
    public Map<String, DicomServer> getDicomServer() {
        HashMap<String, DicomServer> dicomServerMap = Maps.newHashMap();
        for (Integer pacsPort : pacsPorts) {
            dicomServerMap.put("DICOM_SERVER_AT" + pacsPort, DicomServer.init(null, pacsStorageDir, pacsAeTitle, pacsPort, storeService, mwlcFindSUP, cmovesup));
        }
        return dicomServerMap;
    }
}
